{
 "metadata": {
  "signature": "sha256:4f9a033e7680b4b95bcdd556442d53c21329ccf093f34e68f4e843c6d1aeba0d"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Matplotlib: matplotlib and zope\n",
      "======================================================================\n",
      "\n",
      "`0.\u00a0Prerequisites:\u00a0You\u00a0need\u00a0to\u00a0have\u00a0the\u00a0following\u00a0installed\u00a0to\u00a0successfully\u00a0run\u00a0this\u00a0example:\u00a0Zope,\u00a0Matplotlib\u00a0(on\u00a0top\u00a0of\u00a0Zope's\u00a0Python),\u00a0Python\u00a0Image\u00a0Library\u00a0(PIL).\u00a0And\u00a0one\u00a0more\u00a0thing\u00a0-\u00a0probably\u00a0every\u00a0body\u00a0does\u00a0this\u00a0right,\u00a0but\u00a0just\u00a0in\u00a0case\u00a0-\u00a0zope\u00a0instance\u00a0home\u00a0directory\u00a0has\u00a0to\u00a0be\u00a0writable,\u00a0for\u00a0following\u00a0to\u00a0work.`\n",
      "\n",
      "`1.\u00a0Create\u00a0a\u00a0file\u00a0(e.g.\u00a0mpl.py)\u00a0in\u00a0INSTANCEHOME\\Extensions:`"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import matplotlib\n",
      "matplotlib.use('Agg')\n",
      "from pylab import *\n",
      "from os import *\n",
      "from StringIO import StringIO\n",
      "from PIL import Image as PILImage\n",
      "from matplotlib.backends.backend_agg import FigureCanvasAgg\n",
      "def chart(self):\n",
      "    clf()\n",
      "    img_dpi=72\n",
      "    width=400\n",
      "    height=300\n",
      "    fig=figure(dpi=img_dpi, figsize=(width/img_dpi, height/img_dpi))\n",
      "    x=arange(0, 2*pi+0.1, 0.1)\n",
      "    sine=plot(x, sin(x))\n",
      "    legend(sine, \"y=sin x\", \"upper right\")\n",
      "    xlabel('x')\n",
      "    ylabel('y=sin x')\n",
      "    grid(True)\n",
      "    canvas = FigureCanvasAgg(fig)\n",
      "    canvas.draw()\n",
      "    size = (int(canvas.figure.get_figwidth())*img_dpi, int(canvas.figure.get_figheight())*img_dpi)\n",
      "    buf=canvas.tostring_rgb()\n",
      "    im=PILImage.fromstring('RGB', size, buf, 'raw', 'RGB', 0, 1)\n",
      "    imgdata=StringIO()\n",
      "    im.save(imgdata, 'PNG')\n",
      "    self.REQUEST.RESPONSE.setHeader('Pragma', 'no-cache')\n",
      "    self.REQUEST.RESPONSE.setHeader('Content-Type', 'image/png')\n",
      "    return imgdata.getvalue()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "2. Then create an external method in ZMI (e.g. Id -> mplchart, module name -> \n",
      "mpl, function name -> chart).\n",
      "\n",
      "3. Click the Test tab and you should see the sine plot."
     ]
    }
   ],
   "metadata": {}
  }
 ]
}